package cn.xianshu.pet.controller;


import cn.xianshu.basic.exception.BasicException;
import cn.xianshu.basic.util.AxiosResult;
import cn.xianshu.basic.util.PageList;
import cn.xianshu.pet.domain.Pet;
import cn.xianshu.pet.query.PetQuery;
import cn.xianshu.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;


    /**
     * 获取宠物数据
     * @param query
     * @return
     */
    @PostMapping("/pagelist")
    public AxiosResult queryList(@RequestBody PetQuery query){
        try {
            PageList<Pet> pageList = petService.queryList(query);
            return AxiosResult.me().setResultObj(pageList);
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常!");
        }
    }

    @PostMapping("/onsale")
    public AxiosResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AxiosResult.me();
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常!");
        }
    }


    @PostMapping("/offsale")
    public AxiosResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AxiosResult.me();
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常!");
        }
    }


    @PostMapping("/pageListForOnsale")
    public AxiosResult pageListForOnsale(@RequestBody PetQuery query){
        try {
            query.setState(Pet.ONSALE_ID);
            PageList<Pet> pageList = petService.queryList(query);
            return AxiosResult.me().setResultObj(pageList);
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常!");
        }
    }


    /**
     * 获取上线宠物详情
     * @param id
     * @return
     */
    @GetMapping("/selectOnLineById/{id}")
    public AxiosResult selectOnLineById(@PathVariable("id") Long id){
        try {
            return AxiosResult.me().setResultObj(petService.selectOnLineById(id));
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常!");
        }
    }



}
